package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 统计分析-运营数据页面展示
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> resultMap = new HashMap<>();
        // 日期处理
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        // 设置周一为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);
        // 本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // 本周最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        // 本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        // 本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, maxDayOfMonth);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 会员数据
        // 日期
        resultMap.put("reportDate", df.format(today.getTime()));
        // 今日新增会员
        resultMap.put("todayNewMember", memberDao.countByRegTimeRange(today.getTime(), today.getTime()));
        // 本周新增会员
        resultMap.put("thisWeekNewMember", memberDao.countByRegTimeRange(weekFirstDay.getTime(), weekLastDay.getTime()));
        // 本月新增会员
        resultMap.put("thisMonthNewMember", memberDao.countByRegTimeRange(monthFirstDay.getTime(), monthLastDay.getTime()));
        // 总会员
        resultMap.put("totalMember", memberDao.countByRegTimeBefore(df.format(today.getTime())));

        // 预约数据
        // 今日预约人数
        resultMap.put("todayOrderNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), null));
        // 本周预约人数
        resultMap.put("thisWeekOrderNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), today.getTime(), null));
        // 本月预约人数
        resultMap.put("thisMonthOrderNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), today.getTime(), null));
        // 今日到诊人数
        resultMap.put("todayVisitsNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        // 本周到诊人数
        resultMap.put("thisWeekVisitsNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        // 本月到诊人数
        resultMap.put("thisMonthVisitsNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), today.getTime(), Order.ORDERSTATUS_YES));
        // 热门套餐
        resultMap.put("hostSetmeal", orderDao.countByHotSetMeal(4));
        return resultMap;
    }
}
